﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using NHibernate;
using DunSilverlight.Web.Comm;
using System.ServiceModel.Activation;

namespace DunSilverlight.Web.ImportWcf
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“ImportService”。
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class ImportService : IImportService
    {
        private ISessionFactory hbmSessionFactory;

        public ISessionFactory HbmSessionFactory
        {
            get
            {
                if (hbmSessionFactory == null)
                    hbmSessionFactory = SessionFactory.HbmSessionFactory;

                return hbmSessionFactory;
            }
            set
            {
                hbmSessionFactory = value;
            }
        }

        public string ImportArrearRecords(string fileName, int bankId, int year, int period, int orgId, int userId)
        {
            DataImporter imp = new DataImporter();

            ISession sess = HbmSessionFactory.OpenSession();


            try
            {
                imp.ImportOriginalArrearRecord(fileName, bankId, year, period, orgId, userId, sess, null);
                return string.Format("{0} 'Success': '{1}', 'Data': '{2}','Message': '{3}'{4}", "{", "true", "导入成功!", string.Empty, "}");
            }
            catch (Exception ex)
            {
                return string.Format("{0} 'Success': '{1}', 'Data': '{2}','Message': '{3}'{4}", "{", "true", "导入失败!", ex.Message, "}");
            }
            finally
            {
                sess.Close();
            }
        }

        public string ImportReturnRecords(string fileName, int bankId, int orgId, int userId)
        {
            DataImporter imp = new DataImporter();

            ISession sess = HbmSessionFactory.OpenSession();

            try
            {
                imp.ImportOriginalReturnRecord(fileName, bankId, orgId, userId, sess, null);
                return string.Format("{0} 'Success': '{1}', 'Data': '{2}','Message': '{3}'{4}", "{", "true", "导入成功!", string.Empty, "}");
            }
            catch (Exception ex)
            {
                return string.Format("{0} 'Success': '{1}', 'Data': '{2}','Message': '{3}'{4}", "{", "true", "导入失败!", ex.Message, "}");
            }
            finally
            {
                sess.Close();
            }
        }
    }
}
